﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace YiNing.Tools
{
    /// <summary>
    ///1.设置PropertyGrid的PropertySort为Categorized或者CategorizedAlphabetical
    ///2.要排序的类加注该属性[TypeConverter(typeof(PropertiesSortedByClassDefinitionConverter))]
    ///3.如果需要Category下的元素也按照类定义排序,使用[TypeConverter(typeof(CategoriesSortedByClassDefinitionConverter)),CategoriesSortedByClassDefinitionConverter.ElementsSameOrder]
    /// </summary>
    public class CategoriesSortedByClassDefinitionConverter : ExpandableObjectConverter
    {
        /// <summary>
        ///原本以为PropertySort属性为Categorized而不是CategorizedAlphabetical的话，Category下的属性会按照类定义排序。
        ///测试时发现好像有时候还是会乱序。
        /// </summary>
        [AttributeUsage(AttributeTargets.Class)]
        public class ElementsSameOrderAttribute : Attribute
        {
            public bool IsSameOrder { get; set; } = true;
            public ElementsSameOrderAttribute(bool isSameOrder = true)
            {
                IsSameOrder = isSameOrder;
            }
        }
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
            var categoryNames = new List<string>();
            for (int i = 0; i < pdc.Count; i++)
            {
                categoryNames.Add(pdc[i].Category.Replace("\t", ""));
            }
            categoryNames = categoryNames.Distinct().ToList();
            var propertyNames = new List<string>();
            for (int i = 0; i < pdc.Count; i++)
            {
                propertyNames.Add(pdc[i].Name);
                //反射更改每个Category的名字达到效果
                SetValue(pdc[i], "category", pdc[i].Category.PadLeft(pdc[i].Category.Length + categoryNames.Count - categoryNames.IndexOf(pdc[i].Category), '\t'));
            }

            //判断是否强制Category中的元素按照类的定义顺序进行排布
            bool bElementShouldSort = false;
            var contextAttrList = TypeDescriptor.GetAttributes(value).OfType<ElementsSameOrderAttribute>()?.ToList();
            for (int i = 0; i < contextAttrList.Count; i++)
            {
                if (contextAttrList[i] is ElementsSameOrderAttribute element)
                {
                    bElementShouldSort = element.IsSameOrder;
                }
            }

            return bElementShouldSort ? pdc.Sort(propertyNames.ToArray()) : pdc;
        }
        private void SetValue(object self, string fieldName, object value)
        {
            Type type = self.GetType();
            while (type != typeof(object))
            {
                var fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
                var targetField = fields.Find(n => n.Name == fieldName || n.Name == $"<{fieldName}>k__BackingField");
                if (targetField != null)
                {
                    targetField.SetValue(self, value);
                    break;
                }
                type = type.BaseType;
            }
        }
    }

}
